﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace SIS
{
	/// <summary>
	/// Author: hieunv
	/// </summary>
	public partial class Form01 : Form
	{
		private DataAccess.RegisterCourse _registerCourse;
		private DataAccess.RegisterCourse _register;
		private DataAccess.Takes _takes;
		private DataAccess.Section _section;
		private DataAccess.Summaries _summaries;
		private BindingSource _bs1;
		private BindingSource _bsSection;
		public Form01()
		{
			InitializeComponent();
			InitComponents();
			InitEvents();
		}

		public void InitComponents()
		{
			try
			{
				this._bs1 = new BindingSource();

				this._section = new DataAccess.Section();
				this._section.Fill();
				this._bsSection = new BindingSource();
				this._bsSection.DataSource = this._section;
				this._dgvSection.DataSource = this._bsSection;
				this._takes = new DataAccess.Takes();
				this._takes.Fill();

				this._registerCourse = new DataAccess.RegisterCourse();
				this._registerCourse.Fill();
				this._dgvRegisterCourse.DataSource = this._registerCourse;
				this._dgvRegisterCourse.AutoGenerateColumns = false;
				this._dgvRegisteredCourse.AutoGenerateColumns = false;

				this._cbxStudentID.DataSource = DataAccess.RegisterCourse.GetIds();
				this._cbxStudentID.DisplayMember = DataAccess.RegisterCourse.STUDENT_ID;
				this._cbxStudentID.SelectedItem = null;
				this._cbxStudentID.Text = "Student ID";

				this._cbxStudentName.DataSource = DataAccess.RegisterCourse.GetStudentName();
				this._cbxStudentName.DisplayMember = DataAccess.RegisterCourse.STUDENT_NAME;
				this._cbxStudentName.SelectedItem = null;
				this._cbxStudentName.Text = "Student Name";

				this._cbxYear.DataSource = DataAccess.RegisterCourse.GetYear();
				this._cbxYear.DisplayMember = DataAccess.RegisterCourse.YEAR;
				this._cbxYear.SelectedItem = null;
				this._cbxYear.Text = "Year";

				this._cbxSemester.DataSource = DataAccess.RegisterCourse.GetSemester();
				this._cbxSemester.DisplayMember = DataAccess.RegisterCourse.SEMESTER;
				this._cbxSemester.SelectedItem = null;
				this._cbxSemester.Text = "Semester";

				this._cbxCourseID.DataSource = DataAccess.RegisterCourse.GetCourses();
				this._cbxCourseID.DisplayMember = DataAccess.RegisterCourse.COURSE_ID;
				this._cbxCourseID.SelectedItem = null;
				this._cbxCourseID.Text = "Course ID";

				this._cbxDepartment.DataSource = DataAccess.Department.GetDepartmentName();
				this._cbxDepartment.DisplayMember = DataAccess.Department.DEPARTMENT_NAME;
				this._cbxDepartment.SelectedItem = null;
				this._cbxDepartment.Text = "Department";
				this._cbxDepartment.SelectedValueChanged += new EventHandler((sender, e) =>
				{
					this._cbxStudentID2.DataSource = DataAccess.Student.GetStudentIDs(this._cbxDepartment.Text);
					this._cbxStudentID2.DisplayMember = DataAccess.Student.STUDENT_ID;
				});

				this._cbxCourseID2.DataSource = this._section;
				this._cbxCourseID2.DisplayMember = DataAccess.Section.COURSE_ID;
				this._cbxCourseID2.SelectedItem = null;
				this._cbxCourseID2.Text = "Course ID";
				this._cbxCourseID2.SelectedValueChanged += new EventHandler((sender, e) =>
				{


					this._cbxInstructor.DataSource = DataAccess.Instructor.GetInstructorIDs(this._cbxCourseID2.Text);
					this._cbxInstructor.DisplayMember = DataAccess.Instructor.INSTRUCTOR_NAME;
				});

				this._cbxSemester2.DataSource = this._section;
				this._cbxSemester2.DisplayMember = DataAccess.Section.SEMESTER;
				
				this._cbxYear2.DataSource = this._section;
				this._cbxYear2.DisplayMember = DataAccess.Section.YEAR;

				this._cbxBuilding.DataSource = this._section;
				this._cbxBuilding.DisplayMember = DataAccess.Section.BUILDING;

				this._cbxRoomNumber.DataSource = this._section;
				this._cbxRoomNumber.DisplayMember = DataAccess.Section.ROOM_NUMBER;

				this._cbxDepartment4.DataSource = DataAccess.Department.GetDepartmentName();
				this._cbxDepartment4.DisplayMember = DataAccess.Department.DEPARTMENT_NAME;
				this._cbxDepartment4.SelectedValueChanged += new EventHandler((sender, e) =>
				{
					this._cbxStudentID4.DataSource = DataAccess.Student.GetStudentIDs(this._cbxDepartment4.Text);
					this._cbxStudentID4.DisplayMember = DataAccess.Student.STUDENT_ID;
				});
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.ToString());
			}
			
		}

		private void InitEvents()
		{
			#region _btnFilter.Click
			this._btnFilter.Click += new EventHandler((sender, e) =>
			{
				DataAccess.Arguments args = new DataAccess.Arguments();
				if (this._cbxStudentID.SelectedItem != null)
				{

					args.Rows.Add("Id", this._cbxStudentID.Text);
				}
				if (this._cbxStudentName.SelectedItem != null)
				{

					args.Rows.Add("StudentName", this._cbxStudentName.Text);
				}
				if (this._cbxYear.SelectedItem != null)
				{
					args.Rows.Add("Year", this._cbxYear.Text);
				}
				if (this._cbxSemester.SelectedItem != null)
				{
					args.Rows.Add("Semester", this._cbxSemester.Text);
				}
				if (this._cbxCourseID.SelectedItem != null)
				{
					args.Rows.Add("CourseId", this._cbxCourseID.Text);
				}
				this._registerCourse.Fill(args);
			});
			#endregion

			#region _tcContainer.SelectedIndexChanged
			this._tcContainer.SelectedIndexChanged += new EventHandler((sender, e) =>
			{
				if (this._tcContainer.SelectedTab == this._tpRegisterCourse)
				{
					if (this._dgvRegisteredCourse.DataSource == null)
					{
						this._register = new DataAccess.RegisterCourse();
						this._bs1.DataSource = this._register.GetRegister();
						this._dgvRegisteredCourse.DataSource = this._bs1;
					}
				}
			});
            this._tcContainer.SelectedIndexChanged += new EventHandler((sender, e) =>
            {
                if (this._tcContainer.SelectedTab == this._tpSection)
                {
					/*
                    this._cbxCourseID3.DataSource = this._section;
                    this._cbxCourseID3.DisplayMember = DataAccess.Section.COURSE_ID;
                    this._cbxSectionID3.DataSource = this._section;
                    this._cbxSectionID3.DisplayMember = DataAccess.Section.SECTION_ID;
                    this._cbxSemester3.DataSource = this._section;
                    this._cbxSemester3.DisplayMember = DataAccess.Section.SEMESTER;
                    this._cbxYear3.DataSource = this._section;
                    this._cbxYear3.DisplayMember = DataAccess.Section.YEAR;
                    this._cbxBuilding3.DataSource = this._section;
                    this._cbxBuilding3.DisplayMember = DataAccess.Section.BUILDING;
                    this._cbxRoomNumber3.DataSource = this._section;
                    this._cbxRoomNumber3.DisplayMember = DataAccess.Section.ROOM_NUMBER;
                    this._cbxTimeSlotID3.DataSource = this._section;
                    this._cbxTimeSlotID3.DisplayMember = DataAccess.Section.TIME_SLOT_ID;
					//*/
                }
            });
			#endregion

			#region _btnInsert.Click
			this._btnInsert.Click += new EventHandler((sender, e) =>
			{
				try
				{
					Business.Takes take = this._takes.NewRow() as Business.Takes;
					DataRow row = this._section.Rows[this._cbxCourseID2.SelectedIndex];
					take.StudentId = this._cbxStudentID2.Text;
					take.CourseId = (string)row[DataAccess.Section.COURSE_ID];
					take.SectionId = (string)row[DataAccess.Section.SECTION_ID];
					take.Semester = (string)row[DataAccess.Section.SEMESTER];
					take.Year = (int)row[DataAccess.Section.YEAR];
					if (take.Insert())
					{
						this._bs1.DataSource = this._register.GetRegister();
						MessageBox.Show("OK !!!");
					}
				}
				catch (System.Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
				
			});
			#endregion

			#region _btnUpdate.Click
			this._btnUpdate.Click += new EventHandler((sender, e) =>
			{
				try
				{
					DataRowView original = this._bs1.Current as DataRowView;
					Business.Section section = (Business.Section)this._section.Rows[this._cbxCourseID2.SelectedIndex];
					foreach (Business.Takes take in this._takes.Rows)
					{
						if (take.StudentId == (string)original[DataAccess.RegisterCourse.STUDENT_ID] && take.CourseId == (string)original[DataAccess.RegisterCourse.COURSE_ID] && take.SectionId == (string)original[DataAccess.RegisterCourse.SECTION_ID] && take.Semester == (string)original[DataAccess.RegisterCourse.SEMESTER] && take.Year == (int)original[DataAccess.RegisterCourse.YEAR])
						{
							take.StudentId = this._cbxStudentID2.Text;
							take.CourseId = section.CourseID;
							take.SectionId = section.SectionID;
							take.Semester = section.Semester;
							take.Year = section.Year;
							take.Update();
							break;
						}
					}
					this._bs1.DataSource = this._register.GetRegister();
					MessageBox.Show("OK !!!");
				}
				catch (Exception exc)
				{
					MessageBox.Show(exc.ToString());
				}
				
			});
			#endregion

			#region _btnDelete.Click
			this._btnDelete.Click += new EventHandler((sender, e) =>
			{
				try
				{
					DataRowView current = (DataRowView)this._bs1.Current;
					foreach (Business.Takes row in this._takes.Rows)
					{
						if (row.StudentId == (string)current[DataAccess.RegisterCourse.STUDENT_ID] && row.CourseId == (string)current[DataAccess.RegisterCourse.COURSE_ID] && row.SectionId == (string)current[DataAccess.RegisterCourse.SECTION_ID] && row.Semester == (string)current[DataAccess.RegisterCourse.SEMESTER] && row.Year == (int)current[DataAccess.RegisterCourse.YEAR])
						{
							row.Delete();
							break;
						}
					}
					this._bs1.DataSource = this._register.GetRegister();
					MessageBox.Show("OK !!!");
				}
				catch (System.Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
				
			});
			#endregion

			#region 
			this._btnSearch.Click += new EventHandler((sender, e) =>
			{
				if (this._cbxCourseID2.SelectedItem != null)
				{
					this._bs1.DataSource = this._register.GetRegister(this._cbxStudentID2.Text);
				}
			});
			#endregion

			#region 
			this._dgvRegisteredCourse.SelectionChanged += new EventHandler((sender, e) =>
			{
				DataRowView current = this._bs1.Current as DataRowView;
				if (current != null)
				{
					this._cbxCourseID2.Text = (string)current[DataAccess.RegisterCourse.COURSE_ID];
					this._cbxSemester2.Text = (string)current[DataAccess.RegisterCourse.SEMESTER];
					this._cbxYear2.Text = current[DataAccess.RegisterCourse.YEAR].ToString();
					this._cbxBuilding.Text = (string)current[DataAccess.RegisterCourse.BUILDING];
					this._cbxRoomNumber.Text = (string)current[DataAccess.RegisterCourse.ROOM_NUMBER];
					this._cbxInstructor.Text = (string)current[DataAccess.RegisterCourse.INSTRUCTOR_NAME];
					this._cbxDepartment.Text = (string)current[DataAccess.RegisterCourse.DEPARTMENT_NAME];
					this._cbxStudentID2.Text = (string)current[DataAccess.RegisterCourse.STUDENT_ID];
				}
			});
			#endregion

			#region
			this._dgvSection.SelectionChanged += new EventHandler((sender, e) =>
			{
				Business.Section section = this._section.Rows[this._bsSection.CurrencyManager.Position] as Business.Section;
				if (section != null)
				{
					this._cbxCourseID3.Text = section.CourseID;
					this._cbxSectionID3.Text = section.SectionID;
					this._cbxSemester3.Text = section.Semester;
					this._cbxYear3.Text = section.Year.ToString();
					this._cbxBuilding3.Text = section.Building;
					this._cbxRoomNumber3.Text = section.RoomNumber;
					this._cbxTimeSlotID3.Text = section.TimeSlotID;
				}
			});
			#endregion

			#region
			this._btnSearch2.Click += new EventHandler((sender, e) =>
			{
				if (this._summaries == null)
				{
					this._summaries = new DataAccess.Summaries();
				}
				this._summaries.Fill(this._cbxStudentID4.Text);
				this._dgvSummaries.DataSource = this._summaries;

			});
			#endregion

            this._btnInsertSection.Click += new EventHandler((sender, e) =>
            {
                try
                {
                    Business.Section section = this._section.NewRow() as Business.Section;
                    section.CourseID = this._cbxCourseID3.Text;
                    section.SectionID = this._cbxSectionID3.Text;
                    section.Semester = this._cbxSemester3.Text;
                    section.Year = int.Parse(this._cbxYear3.Text);
                    section.Building = this._cbxBuilding3.Text;
                    section.RoomNumber = this._cbxRoomNumber3.Text;
                    section.TimeSlotID = this._cbxTimeSlotID3.Text;
					if (section.Insert())
					{
						MessageBox.Show("OK !!!");
					}
					
                }
				catch (SqlException ex)
				{
					MessageBox.Show(ex.Message);
				}
                catch (Exception ex)
                {
					MessageBox.Show(ex.ToString());
                }
            });
            this._btnUpdateSection.Click += new EventHandler((sender, e) =>
            {
				Business.Section section = this._section.Rows[this._bsSection.CurrencyManager.Position] as Business.Section;
				section.CourseID = this._cbxCourseID3.Text;
				section.SectionID = this._cbxSectionID3.Text;
				section.Semester = this._cbxSemester3.Text;
				section.Year = int.Parse(this._cbxYear3.Text);
				section.Building = this._cbxBuilding3.Text;
				section.RoomNumber = this._cbxRoomNumber3.Text;
				section.TimeSlotID = this._cbxTimeSlotID3.Text;
				if (section.Update())
				{
					MessageBox.Show("OK !!!");
				}
				
            });
            this._btnDeleteSection.Click += new EventHandler((sender, e) =>
            {
                Business.Section section = this._section.Rows[this._bsSection.CurrencyManager.Position] as Business.Section;
				if (section.Delete())
				{
					MessageBox.Show("OK !!!");
				}
            });
        }
	}
}
